﻿''' <summary>
''' This class implements a Ping collector.
''' </summary>
''' <remarks>
''' The purpose of the Ping collector is to send ICMP ping requests to a 
''' list of servers and include the result in the system info report.
''' </remarks>
Public Class PingCollector
    Implements ICollector

    ''' <summary>
    ''' List of writers passed when Startup() is called.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_writers As List(Of Writer.IWriter)

    ''' <summary>
    ''' List of Ping entries.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_entries As List(Of PingEntry)

    ''' <summary>
    ''' This holds an error description.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_error As String

    Public Sub Cleanup() Implements ICollector.Cleanup

        'Nothing to do

    End Sub

    Public Sub Run() Implements ICollector.Run

        'Write our category name.
        Writer.WritersHelper.WriteCategory("Ping Information", m_writers)

        'If an error occurred while reading the configuration file, write
        'it out and stop here.
        If m_error <> "" Then
            Writer.WritersHelper.WriteCategoryError(m_error, m_writers)
        Else
            'Scan all ping entries.
            For Each ping As PingEntry In m_entries
                Dim p As New System.Net.NetworkInformation.Ping
                Try
                    'Write the server name and address.
                    Writer.WritersHelper.WriteSection(ping.Name + " (" + ping.Address + ")", m_writers)
                    'Process this item.
                    Dim pr As System.Net.NetworkInformation.PingReply = p.Send(ping.Address)
                    If pr.Status = Net.NetworkInformation.IPStatus.Success Then
                        Writer.WritersHelper.WriteEntry("Ping succeeded, roundtrip=" + pr.RoundtripTime.ToString() + " ms", m_writers)
                    Else
                        Writer.WritersHelper.WriteSectionError("Ping failed (" + pr.Status.ToString() + ")", m_writers)
                    End If
                Catch ex As Exception
                    'Error gathering info.
                    Writer.WritersHelper.WriteSectionError("Error sending ping (" + ex.Message + ")", m_writers)
                Finally
                    p.Dispose()
                    p = Nothing
                End Try
                'Write out a blank.
                Writer.WritersHelper.WriteBlank(m_writers)
            Next
        End If

    End Sub

    Public Sub Startup(ByVal cfgFile As String, ByVal writers As System.Collections.Generic.List(Of Writer.IWriter)) Implements ICollector.Startup

        'Get all writers.
        m_writers = writers
        'Read configuration file.
        ReadConfiguration(cfgFile)

    End Sub

    ''' <summary>
    ''' Read the configuration file.
    ''' </summary>
    ''' <param name="fName">File name with Ping collector configuration.</param>
    ''' <remarks></remarks>
    Protected Sub ReadConfiguration(ByVal fName As String)

        Const PING_TABLE As String = "PingAddress"

        Try
            'Read in a dataset.
            Dim ds As New DataSet
            ds.ReadXml(fName)
            m_entries = New List(Of PingEntry)

            'Parse all entries.
            For i As Integer = 0 To ds.Tables(PING_TABLE).Rows.Count - 1
                With ds.Tables(PING_TABLE).Rows(i)
                    m_entries.Add(New PingEntry(Convert.ToString(.Item("Name")), _
                                                Convert.ToString(.Item("Address"))))
                End With
            Next

            ds.Dispose()
            ds = Nothing

            'Signal no error.
            m_error = ""
        Catch ex As Exception
            'Keep the error here.
            m_error = ex.ToString()
        End Try

    End Sub

End Class
